package com.situ.mall.web.controller;

import com.situ.mall.common.entity.Member;
import com.situ.mall.common.entity.MemberAddress;
import com.situ.mall.common.service.IMemberAddressService;
import com.situ.mall.common.util.RequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/")
public class UcAddressController {

@Autowired
private IMemberAddressService memberAddressService;
    //个人地址
    @GetMapping("/uc-address")
    public String address(Model model){
        model.addAttribute("MyAddress", memberAddressService.selectAddress());
        return "uc-address";
    }

    @PostMapping("/uc-address/save")
    @ResponseBody
    public void save(MemberAddress memberAddress){
        Member m = (Member)RequestUtil.getCurrent();
        memberAddress.setMemberId(m.getId());
        memberAddressService.saveOrUpdate(memberAddress);
    }

    @GetMapping("/uc-address/select")
    @ResponseBody
    public MemberAddress select(Integer id){
        return memberAddressService.selectById(id);
    }

    @PostMapping("/uc-address/delete")
    @ResponseBody
    public void delete(Integer id){
        memberAddressService.removeById(id);
    }

    @PostMapping("/uc-address/editDefault")
    @ResponseBody
    public void editDefault(Integer id){
        memberAddressService.updateDefault(id);
    }
}
